package cz.uvn.orthopedy.client.ui.editor;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.datepicker.client.DateBox;

import cz.uvn.orthopedy.client.entity.Doctor;
import cz.uvn.orthopedy.client.entity.Hall;
import cz.uvn.orthopedy.client.entity.JournalEntry;
import cz.uvn.orthopedy.client.entity.Operation;
import cz.uvn.orthopedy.shared.FieldVerifier;

public class OperationEditor extends Composite {
	private JournalEntry entry;
	private Hall[] hallsCache;
	private Doctor[] doctorsCache;
	
	private ListBox hallsComboBox;
	private ListBox doctorsBox;
	private DateBox dateLabel;
	private ListBox typeComboBox;
	private TextBox symbolTextBox;
	private TextBox implantTextBox;
	private TextBox diagnosisCodeTextBox;
	private TextArea diagnosisTextArea;
	private ListBox transfusionComboBox;
	private ListBox anestesyComboBox;
	private TextBox internalComplicationsTextBox;
	private CheckBox atbCheckBox;
	private CheckBox rtgCheckBox;
	private CheckBox turnistleCheckBox;
	private CheckBox diabetesCheckBox;
	private CheckBox septicCheckBox;
	private CheckBox pozHbsAgCheckBox;

	public OperationEditor(JournalEntry entry, Hall[] hallsCache, Doctor[] doctorsCache) {
		this.entry = entry;
		this.hallsCache = hallsCache;
		this.doctorsCache = doctorsCache;
		
		initUI();
	}

	private void initUI() {
		AbsolutePanel operationPanel = new AbsolutePanel();
		operationPanel.setSize("461px", "495px");

		Label lblHall = new Label("Operační Sál:");
		lblHall.setStyleName("boldLabel");
		lblHall.setSize("127px", "18px");
		operationPanel.add(lblHall, 10, 14);

		hallsComboBox = new ListBox(false);
		for (int i = 0; i < hallsCache.length; i++) {
			hallsComboBox.addItem(hallsCache[i].toString());
		}
		hallsComboBox.setVisibleItemCount(1);
		hallsComboBox.setSize("302px", "22px");
		operationPanel.add(hallsComboBox, 143, 10);

		Label lblDoctor = new Label("Operatér:");
		lblDoctor.setStyleName("boldLabel");
		lblDoctor.setSize("127px", "18px");
		operationPanel.add(lblDoctor, 10, 42);

		doctorsBox = new ListBox(false);
		for (int i = 0; i < doctorsCache.length; i++) {
			doctorsBox.addItem(doctorsCache[i].toString());
		}
		doctorsBox.setVisibleItemCount(1);
		doctorsBox.setSize("302px", "22px");
		operationPanel.add(doctorsBox, 143, 38);

		Label lblDate = new Label("Datum Zápisu:");
		lblDate.setStyleName("boldLabel");
		lblDate.setSize("127px", "18px");
		operationPanel.add(lblDate, 10, 70);

		dateLabel = new DateBox();
		dateLabel.setStyleName("dateLabelNormal");
		dateLabel.setSize("300px", "18px");
		operationPanel.add(dateLabel, 143, 66);

		Label lblType = new Label("Typ Výkonu:");
		lblType.setStyleName("boldLabel");
		lblType.setSize("127px", "18px");
		operationPanel.add(lblType, 10, 98);

		typeComboBox = new ListBox(false);
		for (Operation.Type type : Operation.Type.values()) {
			typeComboBox.addItem(type.toString());
		}
		typeComboBox.setVisibleItemCount(1);
		typeComboBox.setSize("302px", "22px");
		operationPanel.add(typeComboBox, 143, 94);

		Label lblSymbol = new Label("Značka:");
		lblSymbol.setStyleName("boldLabel");
		lblSymbol.setSize("127px", "18px");
		operationPanel.add(lblSymbol, 10, 128);

		symbolTextBox = new TextBox();
		symbolTextBox.setSize("294px", "16px");
		operationPanel.add(symbolTextBox, 143, 122);

		Label lblImplant = new Label("Implantat:");
		lblImplant.setStyleName("boldLabel");
		lblImplant.setSize("127px", "18px");
		operationPanel.add(lblImplant, 10, 158);

		implantTextBox = new TextBox();
		implantTextBox.setSize("294px", "16px");
		operationPanel.add(implantTextBox, 143, 152);

		Label lblDiagnosisCode = new Label("Kod Diagnózy:");
		lblDiagnosisCode.setStyleName("boldLabel");
		lblDiagnosisCode.setSize("127px", "18px");
		operationPanel.add(lblDiagnosisCode, 10, 188);

		diagnosisCodeTextBox = new TextBox();
		diagnosisCodeTextBox.setSize("294px", "16px");
		operationPanel.add(diagnosisCodeTextBox, 143, 182);

		Label lblDiagnosis = new Label("Diagnóza:");
		lblDiagnosis.setStyleName("boldLabel");
		operationPanel.add(lblDiagnosis, 10, 212);
		lblDiagnosis.setSize("127px", "18px");

		diagnosisTextArea = new TextArea();
		diagnosisTextArea.setSize("296px", "134px");
		operationPanel.add(diagnosisTextArea, 143, 216);

		Label lblTransfusion = new Label("Transfuze:");
		lblTransfusion.setStyleName("boldLabel");
		lblTransfusion.setSize("127px", "18px");
		operationPanel.add(lblTransfusion, 10, 368);

		transfusionComboBox = new ListBox(false);
		for (Operation.Transfusion transfusion : Operation.Transfusion.values()) {
			transfusionComboBox.addItem(transfusion.toString());
		}
		transfusionComboBox.setVisibleItemCount(1);
		transfusionComboBox.setSize("302px", "22px");
		operationPanel.add(transfusionComboBox, 143, 364);

		Label lblAnestesy = new Label("Anestezie:");
		lblAnestesy.setStyleName("boldLabel");
		lblAnestesy.setSize("127px", "18px");
		operationPanel.add(lblAnestesy, 10, 396);

		anestesyComboBox = new ListBox(false);
		for (Operation.Anestesia anestesia : Operation.Anestesia.values()) {
			anestesyComboBox.addItem(anestesia.toString());
		}
		anestesyComboBox.setVisibleItemCount(1);
		anestesyComboBox.setSize("302px", "22px");
		operationPanel.add(anestesyComboBox, 143, 392);

		atbCheckBox = new CheckBox("ATB");
		atbCheckBox.setStyleName("boldLabel");
		operationPanel.add(atbCheckBox, 10, 456);

		rtgCheckBox = new CheckBox("RTG");
		rtgCheckBox.setStyleName("boldLabel");
		operationPanel.add(rtgCheckBox, 63, 456);

		turnistleCheckBox = new CheckBox("Turniket");
		turnistleCheckBox.setStyleName("boldLabel");
		operationPanel.add(turnistleCheckBox, 116, 456);

		diabetesCheckBox = new CheckBox("Diabetes");
		diabetesCheckBox.setStyleName("boldLabel");
		operationPanel.add(diabetesCheckBox, 189, 456);

		septicCheckBox = new CheckBox("Septika");
		septicCheckBox.setStyleName("boldLabel");
		operationPanel.add(septicCheckBox, 266, 456);

		pozHbsAgCheckBox = new CheckBox("PozHbsAg");
		pozHbsAgCheckBox.setStyleName("boldLabel");
		operationPanel.add(pozHbsAgCheckBox, 336, 456);

		Label lblInternalComplikations = new Label("Interní Komplikace:");
		lblInternalComplikations.setStyleName("boldLabel");
		operationPanel.add(lblInternalComplikations, 10, 426);
		lblInternalComplikations.setSize("127px", "18px");

		internalComplicationsTextBox = new TextBox();
		internalComplicationsTextBox.setSize("294px", "16px");
		operationPanel.add(internalComplicationsTextBox, 143, 420);
		
		initWidget(operationPanel);
		
		if (entry != null) {
			for (int i = 0; i < hallsCache.length; i++) {
				if (hallsCache[i].getId().equals(entry.getHallID())) {
					hallsComboBox.setSelectedIndex(i);
					break;
				}
			}
			
			for (int i = 0; i < doctorsCache.length; i++) {
				if (doctorsCache[i].getId().equals(entry.getDoctorID())) {
					doctorsBox.setSelectedIndex(i);
					break;
				}
			}

			dateLabel.setValue(entry.getDate());

			for (int i = 0; i < Operation.Type.values().length; i++) {
				if (Operation.Type.values().equals(entry.getOperation().getType())) {
					typeComboBox.setSelectedIndex(i);
					break;
				}
			}

			symbolTextBox.setText(entry.getOperation().getSymbol());
			implantTextBox.setText(entry.getOperation().getImplant());
			diagnosisCodeTextBox.setText(entry.getOperation().getDiagnosisCode());
			diagnosisTextArea.setText(entry.getOperation().getDiagnosis());

			for (int i = 0; i < Operation.Transfusion.values().length; i++) {
				if (Operation.Transfusion.values().equals(entry.getOperation().getTransfusion())) {
					transfusionComboBox.setSelectedIndex(i);
					break;
				}
			}

			for (int i = 0; i < Operation.Anestesia.values().length; i++) {
				if (Operation.Anestesia.values().equals(entry.getOperation().getAnestesia())) {
					anestesyComboBox.setSelectedIndex(i);
					break;
				}
			}

			internalComplicationsTextBox.setText(entry.getOperation().getInternalComplications());

			atbCheckBox.setValue(entry.getOperation().isAtb());
			rtgCheckBox.setValue(entry.getOperation().isRtg());
			turnistleCheckBox.setValue(entry.getOperation().isTurnistle());
			diabetesCheckBox.setValue(entry.getOperation().isDiabetes());
			septicCheckBox.setValue(entry.getOperation().isSeptic());
			pozHbsAgCheckBox.setValue(entry.getOperation().isPozHbsAg());
		} else {
//			hallsBox.setSelectedIndex(hallsTabBar.getSelectedTab());
		}
	}
	
	public JournalEntry getEntry() {
		Operation operation = entry == null ? new Operation() : entry.getOperation();
		operation.setType(Operation.Type.values()[typeComboBox.getSelectedIndex()]);
		operation.setTransfusion(Operation.Transfusion.values()[transfusionComboBox.getSelectedIndex()]);
		operation.setAnestesia(Operation.Anestesia.values()[anestesyComboBox.getSelectedIndex()]);
		operation.setSymbol(symbolTextBox.getText());
		operation.setDiagnosisCode(diagnosisCodeTextBox.getText());
		operation.setDiagnosis(diagnosisTextArea.getText());
		operation.setImplant(implantTextBox.getText());
		operation.setInternalComplications(internalComplicationsTextBox.getText());
		operation.setAtb(atbCheckBox.getValue());
		operation.setRtg(rtgCheckBox.getValue());
		operation.setTurnistle(turnistleCheckBox.getValue());
		operation.setDiabetes(diabetesCheckBox.getValue());
		operation.setSeptic(septicCheckBox.getValue());
		operation.setPozHbsAg(pozHbsAgCheckBox.getValue());
		
		JournalEntry ent = entry == null ? new JournalEntry() : entry;
		ent.setHallID(hallsCache[hallsComboBox.getSelectedIndex()].getId());
		ent.setDate(dateLabel.getValue());
		ent.setDoctorID(doctorsCache[doctorsBox.getSelectedIndex()].getId());
		if (entry == null) {
			ent.setOperation(operation);
		}
		return ent;
	}

	public boolean isValid() {
		if (FieldVerifier.isDiagnosisCodeCorrect(diagnosisCodeTextBox.getText())) {
			Window.alert("Diagnosis code must have one character and four digits format");
			return false;
		}
		if (diagnosisTextArea.getText().length() < 1) {
			Window.alert("Diagnosis must not be empty");
			return false;
		}
		return true;
	}
}
